import 'package:flutter/cupertino.dart';
import '../config/application.dart';
import '../public.dart';
export 'route_dictionary.dart';

class RouterHelper {
  static NavigatorState get navigator =>
      Application.navigateService.navigator as NavigatorState;

  static Route<dynamic> generateRoute(RouteSettings settings) {
    return CupertinoPageRoute(
      builder: (BuildContext context) {
        String routeName = settings.name as String;
        Object? arguments = settings.arguments;
        RouteHub routeHub = Routers.routeDeictionary[routeName] as RouteHub;

        /// 当前页面 需要登录后才能访问 那么需要重定向页面到登录页面
        if (routeHub.authorize) {
          /// 登录校验
          final token = StorageManager.getItem('token');
          if (token != null) {
            return routeHub.builder(arguments);
          } else {
            RouteHub loginHub =
                Routers.routeDeictionary[Routers.login] as RouteHub;
            return loginHub.builder(routeName);
          }
        }
        return routeHub.builder(arguments);
      },
    );
  }
}
